package com.kakavr.auth.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.kakavr.base.constant.BaseConstant;
import com.kakavr.base.constant.SessionConstant;
import com.kakavr.result.Result;
import com.kakavr.result.constant.MsgConstants;

public abstract class AbstractHandlerInterceptor extends HandlerInterceptorAdapter {
	protected String getTargetURL(HttpServletRequest request) {
		String reqURI = request.getRequestURI();
		return reqURI.substring(reqURI.indexOf("/", 1) + 1);
	}

	/**
	 * 检查请求来源
	 * 
	 * @param referer
	 * @return
	 */
	protected boolean chechReferer(String referer) {
		if (referer != null) {
			String requestReferer = StringUtils.substringBefore(StringUtils.substringBetween(referer, "://", "/"), ":");
			String validRefer = BaseConstant.getValidReferer();
			String[] validReferers = validRefer.split(",");
			if (validReferers.length == 0) {
				return false;
			}
			for (String validReferer : validReferers) {
				if (validReferer.equalsIgnoreCase(requestReferer)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 是否ajax请求
	 * 
	 * @param request
	 * @return
	 */
	protected boolean isAjaxRequest(HttpServletRequest request) {
		String requestType = request.getHeader("X-Requested-With");
		if ("XMLHttpRequest".equalsIgnoreCase(requestType)) {
			return true;
		}
		return false;
	}

	/**
	 * 输出请求结果
	 * 
	 * @param msgCode
	 * @param msgContent
	 * @return
	 */
	protected void wrapResult(int msgCode, String msgContent, HttpServletResponse response) {
		Map<String, Object> msgResult = null;
		try {
			msgResult = Result.errorResult(msgCode, msgContent);
		} catch (Exception e) {
			msgResult = Result.errorResult(msgCode, msgContent);
		}
		Result.outJson(msgResult, response);
	}

	/**
	 * 是否登陆
	 * 
	 * @param session
	 * @return
	 */
	protected boolean isAuthen(HttpSession session) {
		boolean flag = false;
		if (session != null) {
			Object o = session.getAttribute(SessionConstant.USER_TAG);
			if (o != null) {
				@SuppressWarnings("unchecked")
				Map<String, Object> curUser = (Map<String, Object>) o;
				if (null != curUser.get("id")) {
					flag = true;
				}
			}
		}
		return flag;
	}


	/**
	 * 是否是不需要登录的请求
	 * 
	 * @param url
	 * @return
	 */
	protected boolean isOpenUrl(String url) {
		boolean flag = false;
		String noAuthenPrefix = BaseConstant.getNoAuthenPrefix();
		if (null == noAuthenPrefix || noAuthenPrefix.trim().isEmpty()) {
			return flag;
		}
		String[] prefix = noAuthenPrefix.split(",");
		for (String str : prefix) {
			if (url.startsWith(str)) {
				flag = true;
				break;
			}
		}
		return true;
	}

	/**
	 * 未登录处理
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	protected void noAuthen(HttpServletRequest request, HttpServletResponse response) throws IOException {
		if (isAjaxRequest(request)) {
			wrapResult(MsgConstants.MSG_CODE_2001, MsgConstants.MSG_CONTENT_2001, response);
		}
	}
}
